package cn.iocoder.yudao.module.dr.dal.mysql.sourcetag;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.dr.controller.admin.sourcetag.vo.SourceTagPageReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.source.SourceDO;
import cn.iocoder.yudao.module.dr.dal.dataobject.sourcetag.SourceTagDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;

/**
 * 标签管理 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface SourceTagMapper extends BaseMapperX<SourceTagDO> {

    default PageResult<SourceTagDO> selectPage(Long userId,SourceTagPageReqVO reqVO) {
        LambdaQueryWrapperX<SourceTagDO> query = new LambdaQueryWrapperX<SourceTagDO>()
                .likeIfPresent(SourceTagDO::getTagName, reqVO.getTagName())
                .eqIfPresent(SourceTagDO::getTagCode, reqVO.getTagCode())
                .eqIfPresent(SourceTagDO::getParentId, reqVO.getParentId())
                .eqIfPresent(SourceTagDO::getTagDesc, reqVO.getTagDesc())
                .betweenIfPresent(SourceTagDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SourceTagDO::getId);
        if(Objects.nonNull(reqVO.getPublicStatus()) ){
            query.and(wrapper -> {
                wrapper.eq(SourceTagDO::getPublicStatus,reqVO.getPublicStatus());
            });
        }else{
            query.and(wrapper -> {
                wrapper.eq(SourceTagDO::getCreator, String.valueOf(userId))
                        .or().eq(SourceTagDO::getPublicStatus,1);
            });
        }

        return selectPage(reqVO, query);
    }

    List<SourceDO> selectRef(Long tagId);
}